package com.joeyzh.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.joey.ui.widget.EmptyRecyclerView;
import com.joey.ui.widget.EmptyView;
import com.joeyzh.R;
import com.joeyzh.utils.OnSaveListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * Created by Joey on 2018/7/19.
 * 基础清单列表组件基类
 *
 * @author Joey
 */

public abstract class BaseItemListFragment<T, ListVm extends RecyclerViewModel<T>, ListPr extends ListPresenter<ListVm>>
        extends BaseMvvmNoBarFragment<List<T>, ListVm, ListPr> implements FilterResultInterface, OnSaveListener {
    protected String title;

    protected TextView tvListDesc;
    protected LinearLayout llListTitle;
    protected EmptyRecyclerView rvTaskList;
    protected EmptyView emptyView;
    protected SmartRefreshLayout smartLayout;

    protected HashMap<String, String> filterMap;
    protected HashMap<String, Object> baseMap;

    @Override
    public View onChildViewCreate(View view, @Nullable Bundle bundle) {
        View root = View.inflate(getActivity(), R.layout.ifr_fragment_list, null);
        if (getArguments() != null) {
            title = getArguments().getString("title");
        }
        tvListDesc = root.findViewById(R.id.tv_list_desc);
        llListTitle = root.findViewById(R.id.ll_list_title);
        rvTaskList = root.findViewById(R.id.rv_task_list);
        emptyView = root.findViewById(R.id.empty_view);
        smartLayout = root.findViewById(R.id.smart_layout);
        emptyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //数据刷新
                if (presenter != null) {
                    presenter.onLoad(ListPresenter.DEFAULT_PAGE, ListPresenter.DEFAULT_ROWS);
                }
            }
        });
        return root;
    }

    @Override
    public void initData() {
        super.initData();
        presenter.setEmptyLayout(emptyView).onLoad(ListPresenter.DEFAULT_PAGE, ListPresenter.DEFAULT_ROWS);
        onLayoutListener();
    }

    @Override
    public List<T> getEntity() {
        if (entity != null) {
            return entity;
        }
        return new ArrayList<>();
    }

    /**
     * 刷新加载
     */
    public void onLayoutListener() {
        //下拉刷新
        smartLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                presenter.onRefresh(refreshlayout);
            }
        });

        //上拉加载
        smartLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                presenter.onLoadMore(refreshlayout);
                ;
            }
        });
    }

    @Override
    public void filterResult(HashMap<String, String> filterInfo, HashMap<String, Object> baseInfo) {
        baseMap = baseInfo;
        filterMap = filterInfo;
        filterMap.put("PageSize", ListPresenter.DEFAULT_ROWS + "");
        //数据刷新
        if (presenter != null) {
            presenter.onLoad(ListPresenter.DEFAULT_PAGE, ListPresenter.DEFAULT_ROWS);
        }
    }

    @Override
    public void save(boolean upLoadFlag) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    public void addEntity(T entity) {
        viewModel.addEntityBean(entity);
        viewModel.getAdapter().notifyDataSetChanged();
    }

    public void addEntity(int index, T entity) {
        viewModel.addEntityBean(index, entity);
        viewModel.getAdapter().notifyDataSetChanged();
    }

    /**
     * 批量添加数据
     * @param entity
     */
    public void addAllEntity(List<T> entity) {
        viewModel.updateEntity(entity);
    }

    public void setTitle(String title) {
        this.title = title;
        llListTitle.setVisibility(View.VISIBLE);
        tvListDesc.setText(title);
    }

    @Override
    public void clear() {
        viewModel.clearEntity();
    }
}
